<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户端" prop="clientId">
        <el-select v-model="queryParams.clientId" placeholder="请选择客户端">
          <el-option
            v-for="dict in clientOptions"
            :key="dict.id"
            :label="dict.title"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据模型" prop="modelId">
        <el-select v-model="queryParams.clientId" placeholder="请选择数据模型">
          <el-option
            v-for="dict in modelOptions"
            :key="dict.id"
            :label="dict.title"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="权限模式" prop="mode">
        <el-select
          v-model="queryParams.mode"
          placeholder="请选择权限模式"
          clearable
        >
          <el-option
            v-for="dict in dict.type.xdata_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="同步时间">
        <el-date-picker
          v-model="daterangeLastTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.ext_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xdata:pipe:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xdata:pipe:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xdata:pipe:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="pipeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="title" />
      <el-table-column label="客户端" align="center" prop="clientTitle" />
      <el-table-column label="数据模型" align="center" prop="modelTitle" />
      <el-table-column label="权限" align="center" prop="mode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xdata_mode" :value="scope.row.mode" />
        </template>
      </el-table-column>
      <el-table-column
        label="上次同步时间"
        align="center"
        prop="lastTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次同步结果" align="center" prop="lastResult" />
      <el-table-column label="优先级" align="center" prop="sorts" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ext_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xdata:pipe:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xdata:pipe:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通道对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-tabs v-model="tab">
          <el-tab-pane name="base" label="基本信息">
            <el-form-item label="名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入名称" />
            </el-form-item>
            <el-form-item label="客户端" prop="clientId">
              <el-select v-model="form.clientId" placeholder="请选择客户端">
                <el-option
                  v-for="dict in clientOptions"
                  :key="dict.id"
                  :label="dict.title"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="数据模型" prop="modelId">
              <el-select v-model="form.clientId" placeholder="请选择数据模型">
                <el-option
                  v-for="dict in modelOptions"
                  :key="dict.id"
                  :label="dict.title"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="权限" prop="mode">
              <el-select v-model="form.mode" placeholder="请选择权限">
                <el-option
                  v-for="dict in dict.type.xdata_mode"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="优先级" prop="sorts">
              <el-input v-model="form.sorts" placeholder="请输入优先级" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.ext_status"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
            <el-form-item label="同步时间" prop="lastTime" v-if="form.id">
              <el-date-picker
                clearable
                readonly
                v-model="form.lastTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择上次同步时间"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="上次同步结果" prop="lastResult" v-if="form.id">
              <el-input
                v-model="form.lastResult"
                placeholder="请输入上次同步结果"
              />
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane name="read" label="读数据" v-if="form.id">
            <el-form-item label="读取操作" prop="readSql">
              <el-input
                type="textarea"
                rows="12"
                v-model="form.readSql"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane name="check" label="验证查询" v-if="form.id">
            <el-form-item label="查询操作" prop="existSql">
              <el-input
                type="textarea"
                rows="12"
                v-model="form.existSql"
                placeholder="请输入查询操作"
              />
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane name="write" label="写数据" v-if="form.id">
            <el-form-item label="新增操作" prop="insertSql">
              <el-input
                type="textarea"
                rows="7"
                v-model="form.insertSql"
                placeholder="请输入内容"
              />
            </el-form-item>
            <el-form-item label="更新操作" prop="updateSql">
              <el-input
                type="textarea"
                rows="7"
                v-model="form.updateSql"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-tab-pane>
          <!--el-tab-pane v-model="del" title="基本信息">
        <el-form-item label="删除操作" prop="delSql">
          <el-input v-model="form.delSql" placeholder="请输入删除操作" />
        </el-form-item>
        </el-tab-pane -->
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPipe,
  getPipe,
  delPipe,
  addPipe,
  updatePipe,
} from "@/api/xdata/pipe";
import { listClient } from "@/api/xdata/client";
import { listModel } from "@/api/xdata/model";

export default {
  name: "Pipe",
  dicts: ["ext_status", "xdata_mode"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通道表格数据
      pipeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态时间范围
      daterangeLastTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        clientId: null,
        modelId: null,
        mode: null,
        lastTime: null,
        status: null,
      },
      clientOptions: [],
      modelOptions: [],
      tab: "base",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        clientId: [
          { required: true, message: "应用不能为空", trigger: "blur" },
        ],
        modelId: [
          { required: true, message: "模型名称不能为空", trigger: "blur" },
        ],
        mode: [{ required: true, message: "权限不能为空", trigger: "change" }],
        readSql: [
          { required: true, message: "读取操作不能为空", trigger: "blur" },
        ],
        insertSql: [
          { required: true, message: "新增操作不能为空", trigger: "blur" },
        ],
        updateSql: [
          { required: true, message: "更新操作不能为空", trigger: "blur" },
        ],
        existSql: [
          { required: true, message: "查询操作不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    listClient({ pageNum: 1, pageSize: 100 }).then((response) => {
      this.clientOptions = response.rows;
    });
    listModel({ pageNum: 1, pageSize: 100 }).then((response) => {
      this.modelOptions = response.rows;
    });
  },
  methods: {
    /** 查询通道列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeLastTime && "" != this.daterangeLastTime) {
        this.queryParams.params["beginLastTime"] = this.daterangeLastTime[0];
        this.queryParams.params["endLastTime"] = this.daterangeLastTime[1];
      }
      listPipe(this.queryParams).then((response) => {
        this.pipeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        clientId: null,
        modelId: null,
        mode: null,
        readSql: null,
        insertSql: null,
        updateSql: null,
        delSql: null,
        existSql: null,
        lastTime: null,
        lastResult: null,
        sorts: 9,
        status: '1',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeLastTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通道";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getPipe(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改通道";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePipe(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPipe(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除选中的通道数据？")
        .then(function () {
          return delPipe(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "xdata/pipe/export",
        {
          ...this.queryParams,
        },
        `pipe_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
